package main

import (
	"fmt"
	"math"
)

func Sqrt(x float64) float64 {
	z := float64(1)
	fmt.Println("z is ", z)

	for i := 0; i < 10000; i++ {
		z = z - (z*z-x)/2*z
	}

	return z
}

func Pic(dx, dy int) [][]uint8 {
	a := make([][]uint8, dx, dy)

	for i := 0; i < dy; i++ {
		a[i] = make([]uint8, dy)

	}

	fmt.Println("done...")
	return a
}

func WordCount(s string) map[string]int {
	m := make(map[string]int)
	a := strings.Fields(s)

	for i := 0; i < len(a); i++ {
		fmt.Println(a[i])
		v, exsit := m[a[i]]

		if exsit {
			m[a[i]] = v + 1
		} else {
			m[a[i]] = 1
		}
	}

	return m
}

func main() {
	fmt.Println(Sqrt(2), math.Sqrt(2))
}

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	first, second := 0, 1
	return func() int {
		ret := first
		first, second = second, first+second
		return ret
	}
}

// keep print out fibonacci number using function closure.
func main() {
	f := fibonacci()

	for i := 0; i < 5; i++ {
		fmt.Println(f())
	}
}
